package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName RoleController
 * @date 2022/9/13 14:22
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping
    public String index(ModelMap model, HttpServletRequest request) {
        Map<String,Object> filters = getFilters(request);
        PageInfo<Role> page = roleService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "role/index";
    }


    @PreAuthorize("hasAuthority('role.create')")
    @GetMapping("/create")
    public String create(ModelMap model) {
        return "role/create";
    }

    @PreAuthorize("hasAuthority('role.create')")
    @PostMapping("save")
    public String save(Role role){
        roleService.save(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @GetMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id){
        Role role=roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @PreAuthorize("hasAuthority('role.update')")
    @PostMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.delete')")
    @GetMapping("/delete/{id}")
    public String del(@PathVariable Long id){
        roleService.remove(id);
        return "redirect:/role";
    }

    @PreAuthorize("hasAuthority('role.assgin')")
    @GetMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable Long roleId,Model model){

        List<Map<String,Object>> zNodes=permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes",zNodes);
        model.addAttribute("roleId",roleId);
        return "role/assignShow";
    }

    @PreAuthorize("hasAuthority('role.assgin')")
    @PostMapping("assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return "common/successPage";
    }

}
